1   /*
2    * Copyright (C) 2009 The Guava Authors
3    *
4    * Licensed under the Apache License, Version 2.0 (the "License");
5    * you may not use this file except in compliance with the License.
6    * You may obtain a copy of the License at
7    *
8    * http://www.apache.org/licenses/LICENSE-2.0
9    *
10   * Unless required by applicable law or agreed to in writing, software
11   * distributed under the License is distributed on an "AS IS" BASIS,
12   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13   * See the License for the specific language governing permissions and
14   * limitations under the License.
15   */
16  
17  package com.google.common.escape;
18  
19  import com.google.common.annotations.GwtCompatible;
20  import com.google.common.collect.ImmutableMap;
21  import com.google.common.escape.testing.EscaperAsserts;
22  
23  import junit.framework.TestCase;
24  
25  import java.io.IOException;
26  import java.util.Map;
27  
28  /**
29   * @author David Beaumont
30   */
31  @GwtCompatible
32  public class ArrayBasedCharEscaperTest extends TestCase {
33    private static final Map<Character, String> NO_REPLACEMENTS =
34        ImmutableMap.of();
35    private static final Map<Character, String> SIMPLE_REPLACEMENTS =
36        ImmutableMap.of(
37            '\n', "<newline>",
38            '\t', "<tab>",
39            '&', "<and>");
40  
41    public void testSafeRange() throws IOException {
42      // Basic escaping of unsafe chars (wrap them in {,}'s)
43      CharEscaper wrappingEscaper =
44          new ArrayBasedCharEscaper(NO_REPLACEMENTS, 'A', 'Z') {
45            @Override protected char[] escapeUnsafe(char c) {
46              return ("{" + c + "}").toCharArray();
47            }
48          };
49      EscaperAsserts.assertBasic(wrappingEscaper);
50      // '[' and '@' lie either side of [A-Z].
51      assertEquals("{[}FOO{@}BAR{]}", wrappingEscaper.escape("[FOO@BAR]"));
52    }
53    
54    public void testSafeRange_maxLessThanMin() throws IOException {
55      // Basic escaping of unsafe chars (wrap them in {,}'s)
56      CharEscaper wrappingEscaper =
57          new ArrayBasedCharEscaper(NO_REPLACEMENTS, 'Z', 'A') {
58            @Override protected char[] escapeUnsafe(char c) {
59              return ("{" + c + "}").toCharArray();
60            }
61          };
62      EscaperAsserts.assertBasic(wrappingEscaper);
63      // escape everything.
64      assertEquals("{[}{F}{O}{O}{]}", wrappingEscaper.escape("[FOO]"));
65    }
66  
67    public void testDeleteUnsafeChars() throws IOException {
68      CharEscaper deletingEscaper =
69          new ArrayBasedCharEscaper(NO_REPLACEMENTS, ' ', '~') {
70            private final char[] noChars = new char[0];
71            @Override protected char[] escapeUnsafe(char c) {
72              return noChars;
73            }
74          };
75      EscaperAsserts.assertBasic(deletingEscaper);
76      assertEquals("Everything outside the printable ASCII range is deleted.",
77          deletingEscaper.escape("\tEverything\0 outside the\uD800\uDC00 " +
78              "printable ASCII \uFFFFrange is \u007Fdeleted.\n"));
79    }
80  
81    public void testReplacementPriority() throws IOException {
82      CharEscaper replacingEscaper =
83          new ArrayBasedCharEscaper(SIMPLE_REPLACEMENTS, ' ', '~') {
84            private final char[] unknown = new char[] { '?' };
85            @Override protected char[] escapeUnsafe(char c) {
86              return unknown;
87            }
88          };
89      EscaperAsserts.assertBasic(replacingEscaper);
90  
91      // Replacements are applied first regardless of whether the character is in
92      // the safe range or not ('&' is a safe char while '\t' and '\n' are not).
93      assertEquals("<tab>Fish <and>? Chips?<newline>",
94          replacingEscaper.escape("\tFish &\0 Chips\r\n"));
95    }
96  }